home *** CD-ROM | disk | FTP | other *** search
/ Maclife 13 / MACLIFE13-No-93-1996.ISO.7z / MACLIFE13-No-93.ISO / Programming / CodeWarrior Sample Source / MyDemo.(6) / MyDemo.c next >
C/C++ Source or Header  |  1996-01-17  |  6KB  |  246 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.c    -    C Source
  10. #
  11. #    Copyright ゥ 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.c        August 1, 1988
  17. #                SillyBalls.make        August 1, 1988
  18. #
  19. #    This is a very simple sample program that demonstrates how to use Color 
  20. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  21. #    a color window and draw randomly colored ovals in the window.
  22. #    
  23. #    The purpose is to show how to get some initial results with Color QuickDraw.
  24. #    It is a complete program and is very short to be as clear as possible.
  25. #    
  26. #    It does not have an Event Loop.  It is not fully functional in the sense that
  27. #    it does not do all the things you would expect a well behaved Macintosh 
  28. #    program to do, like size the window naturally, have an event loop, use menus, 
  29. #    etc.
  30. #
  31. #    See Sample and TESample for the general structure and MultiFinder techniques that
  32. #    we recommend that you use when building a new application.
  33. #
  34. ------------------------------------------------------------------------------*/
  35.  
  36.     
  37. //    Version 1.0:    6/2/88
  38. //                    7/20/88     DJB    Converted to C
  39. //    
  40. //    purpose        To demonstrate a simple color App using Color QuickDraw.
  41. //                        It draws colored balls in a color window, then uses colored
  42. //                        text inverted in the ball.  The ball location and color is Random.
  43. //                        
  44. //                        This program was written by Bo3b Johnson, 1/88.
  45. //                        
  46. //                        The inverted Bob text was a Skippy Blair special concept,
  47. //                        kept for obvious aesthetic reasons.
  48.  
  49. //MW -cut out some other program descriptions.-
  50.  
  51. //MW ** Metrowerks note **
  52. //   All changed code by Metrowerks is commented by "//MW".
  53. //   There is one type of modification to the original source:
  54. //   ・ Added argument type and return type to function definitions.
  55. //       In order to pass with extended error checking on.
  56. //    
  57. //   8/31/93 JA
  58.  
  59.  
  60. #include <Types.h>
  61. #include <Memory.h>
  62. #include <Quickdraw.h>
  63. #include <Fonts.h>
  64. #include <Events.h>
  65. #include <Menus.h>
  66. #include <Windows.h>
  67. #include <TextEdit.h>
  68. #include <Dialogs.h>
  69. #include <OSUtils.h>
  70. #include <ToolUtils.h>
  71. #include <SegLoad.h>
  72.  
  73. /* Constants */
  74. #define BallWidth        20
  75. #define BallHeight        20
  76. #define BobSize            8        /* Size of text in each ball */
  77.  
  78. /* Globals */
  79. Rect    windRect;
  80.     
  81. /* Prototypes */
  82. void Initialize(void);
  83. void NewBall(void);
  84. void EventLoop(void);
  85.  
  86. // 
  87. //    Main body of program SillyBalls
  88. //
  89.  
  90. //MW specified argument and return type.
  91. void main(void)
  92. {
  93.     Initialize();
  94.     EventLoop();
  95. //    do {
  96. //        NewBall();
  97. //    } while (!Button());
  98.     
  99. }
  100.  
  101. // 
  102. //    Initialize everything for the program, make sure we can run
  103. //
  104. MenuHandle menu1,menu2;
  105. //MW specified argument and return type.
  106. void Initialize(void)
  107. {
  108.     WindowPtr    mainPtr;
  109.     OSErr        error;
  110.     SysEnvRec    theWorld;
  111.         
  112.     //
  113.     //    Test the computer to be sure we can do color.  
  114.     //    If not we would crash, which would be bad.  
  115.     //    If we canユt run, just beep and exit.
  116.     //
  117.  
  118.     error = SysEnvirons(1, &theWorld);
  119.     if (theWorld.hasColorQD == false) {
  120.         SysBeep(50);
  121.         ExitToShell();                    /* If no color QD, we must leave. */
  122.     }
  123.     
  124.     /* Initialize all the needed managers. */
  125.     InitGraf(&qd.thePort);
  126.     InitFonts();
  127.     InitWindows();
  128.     InitMenus();
  129.     TEInit();
  130.     InitDialogs(nil);
  131.     InitCursor();
  132.  
  133.     //
  134.     //    To make the Random sequences truly random, we need to make the seed start
  135.     //    at a different number.  An easy way to do this is to put the current time
  136.     //    and date into the seed.  Since it is always incrementing the starting seed
  137.     //    will always be different.  Donユt for each call of Random, or the sequence
  138.     //    will no longer be random.  Only needed once, here in the init.
  139.     //
  140.     GetDateTime((unsigned long*) &qd.randSeed);
  141.  
  142.     SetMenuBar(GetNewMBar(128));
  143.     DrawMenuBar();
  144.     //
  145.     //    Make a new window for drawing in, and it must be a color window.  
  146.     //    The window is full screen size, made smaller to make it more visible.
  147.     //
  148.     windRect = qd.screenBits.bounds;
  149.     InsetRect(&windRect, 50, 50);
  150.     mainPtr = NewCWindow(nil, &windRect, "¥pBob Land", true, documentProc, 
  151.                         (WindowPtr) -1, false, 0);
  152.         
  153.     SetPort(mainPtr);                        /* set window to current graf port */
  154.     TextSize(BobSize);                        /* smaller font for drawing. */
  155.  
  156. }
  157.  
  158.  
  159. // 
  160. //    NewBall: make another ball in the window at a random location and color.
  161. //
  162.  
  163. //MW -specified argument and return type.-
  164. void NewBall(void)
  165. {
  166.     RGBColor    ballColor;
  167.     Rect        ballRect;
  168.     long int    newLeft,
  169.                 newTop;
  170.     
  171.     // 
  172.     //    Make a random new color for the ball.
  173.     //
  174.     ballColor.red   = Random();
  175.     ballColor.green = Random();
  176.     ballColor.blue  = Random();
  177.     
  178.     // 
  179.     //    Set that color as the new color to use in drawing.
  180.     //
  181.     RGBForeColor (&ballColor);
  182.  
  183.     //    
  184.     //    Make a Random new location for the ball, that is normalized to the window size.  
  185.     //    This makes the Integer from Random into a number that is 0..windRect.bottom
  186.     //    and 0..windRect.right.  They are normalized so that we don't spend most of our
  187.     //    time drawing in places outside of the window.
  188.     //
  189.     newTop = Random();    newLeft = Random();
  190.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  191.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  192.     SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  193.     
  194.     //
  195.     //    Move pen to the new location, and paint the colored ball.
  196.     //
  197.     MoveTo(newLeft, newTop);
  198.     PaintOval (&ballRect);
  199.     
  200.     //
  201.     //    Move the pen to the middle of the new ball position, for the text
  202.     //
  203.     MoveTo(ballRect.left + BallWidth/2 - BobSize, 
  204.         ballRect.top + BallHeight/2 + BobSize/2 -1);
  205.     
  206.     //    
  207.     //    Invert the color and draw the text there.  This wonユt look quite right in 1 bit
  208.     //    mode, since the foreground and background colors will be the same.
  209.     //    Color QuickDraw special cases this to not invert the color, to avoid
  210.     //    invisible drawing.
  211.     //
  212.     InvertColor(&ballColor); 
  213.     RGBForeColor(&ballColor);
  214.     DrawString("¥pBob");
  215. }
  216.  
  217.  
  218. void EventLoop(void)
  219. {
  220.     EventRecord event;
  221.     Boolean    quit = false;
  222.     long    selectID, menuID, itemID;
  223.     
  224.     do {
  225.         if (WaitNextEvent(everyEvent, &event, 10, 0)) {
  226.             switch(event.what) {
  227.             case mouseDown:
  228.                 if (selectID = MenuSelect(event.where)) {
  229.                     menuID = HiWord(selectID);
  230.                     itemID = LoWord(selectID);
  231.                     if (menuID == 129 && itemID == 1)
  232.                         quit = true;
  233.                     HiliteMenu(0);
  234.                 }
  235.                 break;
  236.             case keyDown:
  237.                 break;
  238.             default:
  239.                 NewBall();
  240.                 break;
  241.             }
  242.         }
  243.     } while (!quit);
  244. }
  245.  
  246.